﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using GeneralLibs;
using System.Net.Mail;
using System.Data.SqlClient;

namespace GeneralLibs.Lists
{
    public class DiaBan : DoiTuong
    {
        #region string MaDiaBan property
        public string MaDiaBan
        {
            get
            {
                if (_DataRow["MaDiaBan"] is string) return (string)_DataRow["MaDiaBan"];
                return "";
            }
            set
            {
                _DataRow["MaDiaBan"] = value;
            }
        }
        #endregion

        #region string TenDiaBan property
        public string TenDiaBan
        {
            get
            {
                if (_DataRow["TenDiaBan"] is string) return (string)_DataRow["TenDiaBan"];
                return "";
            }
            set
            {
                _DataRow["TenDiaBan"] = value;
            }
        }
        #endregion

        #region string TenTat property
        public string TenTat
        {
            get
            {
                if (_DataRow["TenTat"] is string) return (string)_DataRow["TenTat"];
                return "";
            }
            set
            {
                _DataRow["TenTat"] = value;
            }
        }
        #endregion

        #region Guid MaDiaBanChaID property
        public Guid MaDiaBanChaID
        {
            get
            {
                if (_DataRow["MaDiaBanChaID"] is Guid) return (Guid)_DataRow["MaDiaBanChaID"];
                else return Guid.Empty;
            }
            set
            {
                _DataRow["MaDiaBanChaID"] = value;
            }
        }
        #endregion

        #region int CapDiaBan property
        public int CapDiaBan
        {
            get
            {
                if (_DataRow["CapDiaBan"] is int) return (int)_DataRow["CapDiaBan"];
                else return 0;
            }
            set
            {
                _DataRow["CapDiaBan"] = value;
            }
        }
        #endregion

        public DiaBan(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucDiaBan : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucDiaBan _Instance;
        public static DanhMucDiaBan Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucDiaBan();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "DiaBan";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new DiaBan((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public DiaBan this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new DiaBan(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public DiaBan NewDoiTuong()
        {
            try
            {
                return new DiaBan(Instance.DataTable.NewRow());
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public DiaBan GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new DiaBan(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public DiaBan GetByID(Guid objID)
        {
            try
            {
                return GetByProperty("ID", objID);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public DanhMucDiaBan GetDiaBanCapTinh()
        {
            try
            {
                return Filter(string.Format("MaDiaBanChaID = '{0}' OR MaDiaBanChaID is NULL", Guid.Empty));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public DataTable GetDiaBanCapTinhHuyen()
        {
            SqlConnection con = new SqlConnection(LocalSettings.ConnectString);

            try
            {
                con.Open();

                SqlCommand cmd = con.CreateCommand();
                cmd.CommandText = string.Format("SELECT * FROM DiaBan WHERE  MaDiaBanChaID IN ( \n" +
                                    "   SELECT ID FROM DiaBan \n" +
                                    "	WHERE MaDiaBanChaID is null or MaDiaBanChaID = '{0}' \n" +
                                    ") OR ID IN ( \n" +
                                    "	SELECT ID FROM DiaBan \n" +
                                    "	WHERE MaDiaBanChaID is null or MaDiaBanChaID = '{0}' ) ", Guid.Empty);
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
            finally
            {
                con.Close();
            }
        }

        public DanhMucDiaBan GetDiaBanCapPhuongXa()
        {
            try
            {
                return Filter("CapDiaBan = 3");
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucDiaBan Filter(string filterExpression)
        {
            try
            {
                DanhMucDiaBan obj = new DanhMucDiaBan();
                obj.DataView = new DataView(DanhMucDiaBan.Instance.DataTable, filterExpression, "", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public DanhMucDiaBan GetByMaDiaBanCha(Guid MaDiaBanChaID)
        {
            try
            {
                return Filter(string.Format("MaDiaBanChaID = '{0}'", MaDiaBanChaID));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucDiaBan GetAll()
        {
            try
            {
                return Instance;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}